/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.iocoder.yudao.module.system.tcpserver.protocol2;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;


@Component
@RequiredArgsConstructor
public class NewServerChannelInitializer extends ChannelInitializer<SocketChannel> {

//    private static final int MAX_FRAME_LENGTH = 1024;
//    private static final int LENGTH_FIELD_OFFSET = 10;
//    private static final int LENGTH_ADJUSTMENT = 2;//数据域后面的位数
//    private static final int INITIAL_BYTES_TO_STRIP = 0;

    private static final int MAX_FRAME_LENGTH = 1024 * 1024;  //最大长度
    private static final int LENGTH_FIELD_LENGTH = 2;  //长度字段所占的字节数
    private static final int LENGTH_FIELD_OFFSET = 2;  //长度偏移
    private static final int LENGTH_ADJUSTMENT = -4;
    private static final int INITIAL_BYTES_TO_STRIP = 0;

    private final NewProtocolServerHandler newProtocolServerHandler;

    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();
        //入参说明: 读超时时间、写超时时间、所有类型的超时时间、时间格式
//        pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS));

        pipeline.addLast(new NewZkProtocolDecoder(MAX_FRAME_LENGTH, LENGTH_FIELD_OFFSET, LENGTH_FIELD_LENGTH,
                LENGTH_ADJUSTMENT, INITIAL_BYTES_TO_STRIP, false));

        pipeline.addLast(new NewZkProtocolEncoder());

        pipeline.addLast(newProtocolServerHandler);
//        pipeline.addLast(new IdleStateHandler(10, 0, 0, TimeUnit.SECONDS));
//        pipeline.addLast("heartbeatHandler", new ServerHeartbeatHandler());
    }
}
